package cn.bnuz.edu.todolist;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.bnuz.edu.todolist.database.TodoDB;
import cn.bnuz.edu.todolist.pojo.Event;

public class TodoListPlanActivity extends AppCompatActivity {
    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todo_list_plan_activity);

        List<Event> list = TodoDB.getEvent();
        if (list == null){
            Toast.makeText(this,R.string.list_null_error,Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(TodoListPlanActivity.this,MainActivity.class);
            startActivity(intent);
        }else {
            ListView listView = (ListView)findViewById(R.id.todo_list_plan);
            List<Map<String, Object>> listItem = new ArrayList<>();

            for (Event e: list) {
                if (e.getEventType() == 2) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("标题", e.getEventTitle());
                    map.put("时间", e.getEventTime());
                    map.put("状态", e.isCompleted());
                    listItem.add(map);
                }
            }

            SimpleAdapter adapter = new SimpleAdapter(this, listItem, R.layout.todo_list_event_item, new String[]{"标题", "时间"}, new int[]{R.id.event_name, R.id.event_time});
            listView.setAdapter(adapter);

            System.out.println("************");
            for (int i = 0; i < listView.getCount(); i++) {

            }
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    Map<String, Object> map = (Map<String, Object>)adapterView.getItemAtPosition(position);
                    CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkbox);
                    Boolean checked = (Boolean) map.get("状态");
                    map.put("状态", !checked);
                    checkBox.setChecked(checked);
                }
            });
        }

    }
}
